﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDBConnection
{
    public class ToursDAO
    {
        private static TouristAgencyDataContext context = new TouristAgencyDataContext();

        public static void CreateTour(string destination, DateTime startDate, DateTime endDate, decimal price, string description = null)
        {
            Tour tour = new Tour();
            tour.Destination = destination;
            tour.StartDate = startDate;
            tour.EndDate = endDate;
            tour.Price = price;
            tour.Description = description;
            context.Tours.InsertOnSubmit(tour);
            context.SubmitChanges();
        }

        public static void DeleteTour(Tour tour)
        {
            context.Tours.DeleteOnSubmit(tour);
            context.SubmitChanges();
        }

        public static void ModifyTour(Tour t, string destination = null, DateTime? startDate = null, DateTime? endDate = null, decimal? price = null, string description = null)
        {
            if (destination != null)
            {
                t.Destination = destination;
            }
            if (startDate != null)
            {
                t.StartDate = (DateTime)startDate;
            }
            if (endDate != null)
            {
                t.EndDate = (DateTime)endDate;
            }
            if (price != null)
            {
                t.Price = (decimal)price;
            }
            if (description != null)
            {
                t.Description = description;
            }
            context.SubmitChanges();
        }

        public static IEnumerable<Tour> FindTour(string destination = null, decimal minPrice = 0, decimal maxPrice = Decimal.MaxValue, DateTime? startDate = null, DateTime? endDate = null, string tourUniqueReference = null)
        {
            IEnumerable<Tour> tours = (from t in context.Tours
                                       where (destination == null || t.Destination == destination)
                                        && (t.Price >= minPrice && t.Price <= maxPrice)
                                        && (startDate == null || t.StartDate >= startDate)
                                        && (endDate == null || t.EndDate <= endDate)
                                        && (tourUniqueReference == null || t.TourUniqueReference == tourUniqueReference)
                                       select t).ToList<Tour>();
            return tours;
        }

        public static IEnumerable<Tour> GetAllTours()
        {
            IEnumerable<Tour> tours = (from t in context.Tours
                                       select t).ToList<Tour>();
            return tours;
        }
    }
}
